package com.qf.filter;

import com.qf.entity.EndUser;
import com.qf.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//@WebFilter("/*")
public class BlockerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;

        List<String> urlList = new ArrayList<>();
        urlList.add("pay_return.html");
        urlList.add("paypage.html");
        urlList.add("settleCount.html");
        urlList.add("order.jsp");


       // System.out.println(httpRequest.getRequestURL().toString());
//        Boolean is = false;
        for (String url: urlList) {
            String requestURL = httpRequest.getRequestURL().toString();
            if (requestURL.contains(url)){
                User user = (User) httpRequest.getSession().getAttribute("user");
                if (user == null || user.getUName()==null || user.getUPassword() == null){
                    httpResponse.sendRedirect("login.jsp");
                    return;
                }

            }
        }
            //System.out.println(httpRequest.getAttribute("userName"));
            filterChain.doFilter(servletRequest,servletResponse);
        //这里逻辑错误，不要，二选一  这里如果走else 说明本请求因该是白名单中的啊，上面再放行，下面又重定向乱套了
//        else{
//
//        }


    }

    @Override
    public void destroy() {

    }
}
